<?php


namespace app\api\controller;


use app\service\apiService;
use app\service\cxyunService;
use app\service\invoiceService;
use app\service\orderService;
use think\Controller;
use think\Db;

class CxyunPay extends Controller
{
    public function calculatingTempCost(){
        
        $data["parkingId"]="5369467354105601639";
        $data["plateNumber"]=request()->get("plateNumber");
        $data["timestamp"]=time()."000";
        $notData=array();
        $cxyunApi=new cxyunService();
        $res=$cxyunApi->commonPost("calculatingTempCost",$data,$notData);

        $result= json_decode($res,true);
        if($result["status"]==0){
            return json(["code"=>0,"msg"=>$result["message"]]);
        }else{

        }
        return  $result;
    }
    public function createOrder(){
        $id=request()->post("id");
        $payinfo=Db::table("cxyun_query_order_log")->where("id",$id)->find();
        if(empty($payinfo)){
            json(["code"=>0,"msg"=>"查询账单失败"]);
        }


        //$out_trade_no=date('YmdHiss').rand(100000,999999);
        $out_trade_no=date('YmdHiss').rand(100000,999999);
        
        $token = (new orderService())->gettoken($payinfo["parkingNo"]);
        $subject = $payinfo["parkingName"]."-".$payinfo["plateNumber"];
        $api = new apiService();

        $parkinginfo=Db::table("parkinglotinfo")->where("parkingNo",$payinfo["parkingNo"])->find();

        $notifyUrl="https://www.aliwgzf.com/api/cxyun_notice/alinotice";
       
        $start_time=date("Y-m-d H:i:s",strtotime($payinfo["inTime"]));
        $end_time=date("Y-m-d H:i:s");
        //获取发票信息
        $invoiceinfo=(new invoiceService())->parkingInvoice($payinfo["parkingNo"]);
        $is_support_invoice="false";
        if($invoiceinfo["is_support_invoice"]=="1"){
            $is_support_invoice="true";
        }
        $invoice_merchant_name=$invoiceinfo["invoice_merchant_name"];
        $tax_num=$invoiceinfo["taxrate"];
        $payinfo["upayFee"]=0.01;
        //orderCreate
        $result = $api->orderCreate(json_encode(['out_trade_no'=> $out_trade_no,'total_amount'=>$payinfo["upayFee"],'buyer_id'=>$payinfo["user_id"],'subject' =>$subject,'token'=>$token ,'notifyurl'=>$notifyUrl,"car_number"=>$payinfo["plateNumber"],"start_time"=>$start_time,"parking_lot_id"=>$parkinginfo["parking_id"],"parkling_lot_name"=>$parkinginfo["parkingName"],"city_code"=>$parkinginfo["city_code"],'is_support_invoice'=>$is_support_invoice,'invoice_merchant_name'=>$invoice_merchant_name,'tax_num'=>$tax_num]));
        //Log::write($result,"yangzhanlei");
        if(property_exists($result,'sub_msg')){
            return json(["code"=>500,"msg"=>$result->sub_msg]);
           
        }else{
            $info["tradeNo"]=$result->trade_no;
            $info["outTradeNo"]=$out_trade_no;
            $info["orderNo"]=$payinfo["orderNo"];
            $info["parkingNo"]=$payinfo["parkingNo"];
            $info["parkingId"]=$payinfo["parkingId"];
            $info["parkingName"]=$payinfo["parkingName"];
            $info["plateNumber"]=$payinfo["plateNumber"];
            $info["totalFee"]=$payinfo["totalFee"];
            $info["payFee"]=$payinfo["upayFee"];
            $info["inTime"]=$payinfo["inTime"];
            $info["stopTime"]=$payinfo["stopTime"];
            $info["createTime"]=date("Y-m-d H:i:s");
            $info["user_id"]=$payinfo["user_id"];
            $res=Db::table("cxyun_order")->insert($info);
            if($res){
                return json(["code"=>0,"msg"=>"成功","trade_no"=>$info["tradeNo"]]);
            }else{
                return json(["code"=>500,"msg"=>"订单创建失败"]);
            }
            return json_encode($result);
        }
    }

}